package com.jinduplan.planmanager.ui.activity.report

import android.net.Uri
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.cxz.kotlin.baselibs.adapter.MultiViewHolder
import com.cxz.kotlin.baselibs.adapter.MultiViewRecycleAdapter
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.base.getScreenWidth
import com.cxz.kotlin.baselibs.base.lubanCompress
import com.cxz.kotlin.baselibs.base.setTextChageListener
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.cxz.kotlin.baselibs.utils.UiUtils
import com.cxz.kotlin.baselibs.widget.PhotoSelectDialog
import com.fphoenixcorneae.util.toast.ToastUtil
import com.huantansheng.easyphotos.callback.SelectCallback
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.ReportDetailData
import com.jinduplan.planmanager.bean.ReportListRecord
import com.jinduplan.planmanager.constant.Constant
import com.jinduplan.planmanager.mvp.contract.ReportContract
import com.jinduplan.planmanager.mvp.presenter.ReportPresenter
import com.cxz.kotlin.baselibs.utils.DensityUtil
import com.cxz.kotlin.baselibs.utils.LogUtil
import com.jinduplan.planmanager.utils.ResUtil
import com.jinduplan.planmanager.widgets.TipsDialog
import kotlinx.android.synthetic.main.activity_corrupt_report.*
import kotlinx.android.synthetic.main.activity_corrupt_report_edit.*
import kotlinx.android.synthetic.main.activity_rcy_refresh.*
import kotlinx.android.synthetic.main.activity_register.*
import java.io.File

class ReportEditAcitivity :
    BaseMvpTitleActivity<ReportContract.View, ReportContract.Presenter>(), ReportContract.View {

    val datas = ArrayList<String>()
    val successPics = ArrayList<String>()
    val pic =
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2Ff6%2Fc9%2Ff6%2Ff6c9f647a533782026c0609ac5d550df.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642907245&t=84ef66885758c004c48776283afe28f4"

    var type = "1"
    val dialog by lazy {
        PhotoSelectDialog(this@ReportEditAcitivity, object : SelectCallback() {
            override fun onResult(
                photos: java.util.ArrayList<Photo>,
                isOriginal: Boolean
            ) {
                photos.forEach {
                    datas.add(it.path)
                }

                rcyPics.adapter?.notifyDataSetChanged()
            }

            override fun onCancel() {

            }

        })
    }

    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_corrupt_report_edit
    }


    override fun createPresenter(): ReportContract.Presenter {
        return ReportPresenter()
    }

    override fun start() {
        setBaseTitle(ResUtil.getS(R.string.corrupt_report))
        val hashMap = HashMap<Int, Int>()
        hashMap.put(MultiViewRecycleAdapter.TYPE_1, R.layout.item_corrupt_pic)
        hashMap.put(MultiViewRecycleAdapter.TYPE_2, R.layout.item_pic_del)
        datas.add(pic)
        intent?.extras?.getString(Constant.REPORT_TYPE)?.let {
            type = it
        }
        etContent.setTextChageListener{
            btnReport.isEnabled = !etJob.text.isNullOrEmpty() && !etName.text.isNullOrEmpty()
        }
        etJob.setTextChageListener{
            btnReport.isEnabled = !etContent.text.isNullOrEmpty() && !etName.text.isNullOrEmpty()
        }
        etName.setTextChageListener{
            btnReport.isEnabled = !etContent.text.isNullOrEmpty() && !etJob.text.isNullOrEmpty()
        }
        var screenWidth = this.getScreenWidth()
        screenWidth = (screenWidth - DensityUtil.dip2px(this,56f))/4
        rcyPics.adapter = object : MultiViewRecycleAdapter<String>(this, hashMap, datas) {
            override fun convert(
                holder: MultiViewHolder,
                bean: String,
                position: Int,
                viewType: Int
            ) {
                UiUtils.setViewWideHeightWithinChange(this@ReportEditAcitivity,holder.itemView,screenWidth,screenWidth)
                when (viewType) {
                    TYPE_1 -> {
                        holder.itemView.setOnClickListener {
                            if (datas.size >= 6) {
                                ToastUtil.show("最多选择5张图片")
                                return@setOnClickListener
                            }
                            dialog.setCustomClick(6 - datas.size).show()
                        }
                    }
                    TYPE_2 -> {
                        holder.getView<ImageView>(R.id.ivPic).setImageURI(Uri.fromFile(File(bean)))
                        holder.getView<ImageView>(R.id.ivClose).setOnClickListener {
                            datas.removeAt(position)
                            rcyPics.adapter?.notifyDataSetChanged()
                        }
                    }
                }
            }

            override fun getCustomItemViewType(position: Int): Int {
                if (position == 0) {
                    return TYPE_1
                } else {
                    return TYPE_2
                }
            }

        }
        rcyPics.layoutManager = GridLayoutManager(this, 4)

//        rcyPics.addItemDecoration(object : UniversalItemDecoration() {
//            override fun getItemOffsets(position: Int): Decoration {
//
//                val decoration = ColorDecoration()
//                if (position > 3) {
//                    decoration.decorationColor = ResUtil.getC(R.color.transparent)
//                    decoration.top =
//                        ResUtil.getDimen(this@ReportEditAcitivity, R.dimen.dp_18).toInt()
//                }
//                return decoration
//            }
//        })

        TipsDialog(this).setContent(ResUtil.getS(R.string.corrupt_tips)).show()
        btnReport.setOnClickListener {
            if (etContent.text.length < 10) {
                ToastUtil.show("内容最少10个字符或中文字")
                return@setOnClickListener
            }
            successPics.clear()
            if (datas.size > 1) {
                datas[picsIndex].lubanCompress(this,{
                    mPresenter?.uploadPics(it)
                })
            } else {
                mPresenter?.report(etName.text.toString(),etJob.text.toString(),etContent.text.toString(),type,successPics.toTypedArray())
            }
        }

    }
    var picsIndex = 1

    override fun onPause() {
        super.onPause()
        dialog.dismiss()
    }


    override fun getDetail(data: ReportDetailData) {
        TODO("Not yet implemented")
    }

    override fun reportSuccess() {
        ToastUtil.show("举报成功")
        this.finish()
    }

    override fun uploadSuccess(url: String) {
        LogUtil.d("url-->$url")
        needShowLoading = false
        successPics.add(url)
        picsIndex++
        if (picsIndex < datas.size) {
            datas[picsIndex].lubanCompress(this,{
                mPresenter?.uploadPics(it)
            })
        } else {
            mPresenter?.report(etName.text.toString(),etJob.text.toString(),etContent.text.toString(),type,successPics.toTypedArray())
        }
    }

    override fun refreshReportList(data: ReportListRecord) {
        TODO("Not yet implemented")
    }

    override fun loadReportList(data: ReportListRecord) {
        TODO("Not yet implemented")
    }

}